﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//new
using System.Collections;
using DataRecv.models.device;
using DataRecv.models.road;
using JRCore.tools;
using JRCore.enums;
using JRCore.protocol;
using DataRecv.enums;

namespace DataRecv
{
    public partial class camera : Form
    {
        private Client synclient;
        public camera()
        {
            InitializeComponent();
        }

        private void camera_Load(object sender, EventArgs e)
        {
            try
            {
                dataGridView1.Rows.Clear();
                if (G.Views != null)
                {
                    IDictionaryEnumerator ide = G.Views.GetEnumerator();
                    ide.Reset();
                    while (ide.MoveNext())
                    {
                        DeviceParam d = ((DeviceView)ide.Value).deviceparam;
                        RoadParam r = ((DeviceView)ide.Value).roadparam;
                        dataGridView1.Rows.Add(new object[] { d.roadid, r.roadname, d.ip, d.type.ToString(), d.workmode.ToString(), d.direction.ToString(), d.sbbh, d.speedlimit, d.deviceusername, d.devicepassword, d.mutiroad.ToString(), d.channel1roadid, d.channel2roadid, d.channel3roadid, d.channel4roadid, d.channel5roadid, d.channel6roadid, d.channel7roadid, d.channel8roadid, d.djsnapbyplate.ToString(), d.platetypesnapby.ToString(), d.djsnapbytime.ToString(), d.time1_start, d.time1_end, d.time2_start, d.time2_end, d.time3_start, d.time3_end, d.time4_start, d.time4_end,d.staticpicenable.ToString(),d.staticpicindex.ToString(),d.staticpic, d.bz,d.picwidth.ToString(),d.picheight.ToString() });
                    }
                }

                if ((G.SystemParam.SynServerIP != "") && (G.SystemParam.SynServerPort != ""))
                {
                    synclient = new Client(G.SystemParam.SynServerIP, int.Parse(G.SystemParam.SynServerPort), Enum_TcpWorkMode.SendRead,0);
                    synclient.OnRecordReceived = new Client.RecordReceived(OnDeviceSynFunc);
                }
            }
            catch { }
        }

        private void button_add_Click(object sender, EventArgs e)
        {
            addcamera form_addcam = new addcamera();
            form_addcam.Owner = this;
            form_addcam.ShowDialog();
            form_addcam.Dispose();
        }

        private void button_edit_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.SelectedRows.Count < 1)
            {
                MessageBox.Show("未选择要修改的记录");
            }
            else
            {
                editcamera form_editcam = new editcamera();
                form_editcam.Owner = this;
                form_editcam.rowindex = this.dataGridView1.SelectedRows[0].Index;
                form_editcam.ip = this.dataGridView1.SelectedRows[0].Cells["ip"].Value.ToString();
                form_editcam.roadid = this.dataGridView1.SelectedRows[0].Cells["roadid"].Value.ToString();
                form_editcam.sbbh = this.dataGridView1.SelectedRows[0].Cells["sbbh"].Value.ToString();
                form_editcam.roadname = this.dataGridView1.SelectedRows[0].Cells["roadname"].Value.ToString();
                form_editcam.type = this.dataGridView1.SelectedRows[0].Cells["type"].Value.ToString();
                form_editcam.workmode = this.dataGridView1.SelectedRows[0].Cells["workmode"].Value.ToString();
                form_editcam.direction = this.dataGridView1.SelectedRows[0].Cells["direction"].Value.ToString();
                form_editcam.speedlimit = this.dataGridView1.SelectedRows[0].Cells["speedlimit"].Value.ToString();
                form_editcam.deviceusername = this.dataGridView1.SelectedRows[0].Cells["deviceusername"].Value.ToString();
                form_editcam.devicepassword = this.dataGridView1.SelectedRows[0].Cells["devicepassword"].Value.ToString();
                form_editcam.mutiroad = this.dataGridView1.SelectedRows[0].Cells["mutiroad"].Value.ToString();
                form_editcam.channel1roadid = this.dataGridView1.SelectedRows[0].Cells["channel1roadid"].Value.ToString();
                form_editcam.channel2roadid = this.dataGridView1.SelectedRows[0].Cells["channel2roadid"].Value.ToString();
                form_editcam.channel3roadid = this.dataGridView1.SelectedRows[0].Cells["channel3roadid"].Value.ToString();
                form_editcam.channel4roadid = this.dataGridView1.SelectedRows[0].Cells["channel4roadid"].Value.ToString();
                form_editcam.channel5roadid = this.dataGridView1.SelectedRows[0].Cells["channel5roadid"].Value.ToString();
                form_editcam.channel6roadid = this.dataGridView1.SelectedRows[0].Cells["channel6roadid"].Value.ToString();
                form_editcam.channel7roadid = this.dataGridView1.SelectedRows[0].Cells["channel7roadid"].Value.ToString();
                form_editcam.channel8roadid = this.dataGridView1.SelectedRows[0].Cells["channel8roadid"].Value.ToString();
                form_editcam.djsnapbyplate = this.dataGridView1.SelectedRows[0].Cells["djsnapbyplate"].Value.ToString();
                form_editcam.platetypesnapby = this.dataGridView1.SelectedRows[0].Cells["platetypesnapby"].Value.ToString();
                form_editcam.djsnapbytime = this.dataGridView1.SelectedRows[0].Cells["djsnapbytime"].Value.ToString();
                form_editcam.time1_start = this.dataGridView1.SelectedRows[0].Cells["time1_start"].Value.ToString();
                form_editcam.time1_end = this.dataGridView1.SelectedRows[0].Cells["time1_end"].Value.ToString();
                form_editcam.time2_start = this.dataGridView1.SelectedRows[0].Cells["time2_start"].Value.ToString();
                form_editcam.time2_end = this.dataGridView1.SelectedRows[0].Cells["time2_end"].Value.ToString();
                form_editcam.time3_start = this.dataGridView1.SelectedRows[0].Cells["time3_start"].Value.ToString();
                form_editcam.time3_end = this.dataGridView1.SelectedRows[0].Cells["time3_end"].Value.ToString();
                form_editcam.time4_start = this.dataGridView1.SelectedRows[0].Cells["time4_start"].Value.ToString();
                form_editcam.time4_end = this.dataGridView1.SelectedRows[0].Cells["time4_end"].Value.ToString();
                form_editcam.staticpicenable = this.dataGridView1.SelectedRows[0].Cells["staticpicenable"].Value.ToString();
                form_editcam.staticpicindex = this.dataGridView1.SelectedRows[0].Cells["staticpicindex"].Value.ToString();
                form_editcam.staticpic = this.dataGridView1.SelectedRows[0].Cells["staticpic"].Value.ToString();
                form_editcam.bz = this.dataGridView1.SelectedRows[0].Cells["bz"].Value.ToString();
                form_editcam.picwidth = this.dataGridView1.SelectedRows[0].Cells["picwidth"].Value.ToString();
                form_editcam.picheight = this.dataGridView1.SelectedRows[0].Cells["picheight"].Value.ToString();
                form_editcam.ShowDialog();
                form_editcam.Dispose();
            }

        }

        private void button_delete_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.SelectedRows.Count < 1)
            {
                MessageBox.Show("未选择要删除的记录");
            }
            else
            {
                string ip = this.dataGridView1.SelectedRows[0].Cells["ip"].Value.ToString();
                if (DeviceParam.DeleteModel(ip) == true)
                {
                    if (G.Views != null)
                    {
                        G.Views.Remove(ip);
                    }
                    this.dataGridView1.Rows.Remove(this.dataGridView1.SelectedRows[0]);
                    ((Form_Main)this.Owner).UpDateTree();
                    MessageBox.Show("删除成功");
                }
                else
                {
                    MessageBox.Show("删除失败");
                }
                
            }
        }

        private void button_cancle_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void camera_FormClosing(object sender, FormClosingEventArgs e)
        {
            MessageBox.Show("如参数有修改，重启软件后生效");
            e.Cancel = false;
        }

        private void button_tongbu_Click(object sender, EventArgs e)
        {
            try
            {
                if ((G.Views != null) && (G.Views.Count > 0))
                {
                    MessageBox.Show("同步前需清空所有路口");
                    return;
                }

                if (synclient != null)
                {
                    byte[] DataRecvIP = Encoding.UTF8.GetBytes(G.SystemParam.LocalIP);

                    JRCore.protocol.Msg synrequest = new JRCore.protocol.Msg();
                    synrequest.type = Enum_ProtocolType.DeviceRequest;
                    synrequest.uuid = Guid.NewGuid().ToString("N");
                    synrequest.feedback = true;
                    synrequest.data = DataRecvIP;
                    synrequest.length = synrequest.data.Length;

                    string error = "";
                    bool flag = synclient.Send(synrequest, ref error);
                    if (flag == true)
                    {
                        this.dataGridView1.Rows.Clear();
                        IDictionaryEnumerator ide = G.Views.GetEnumerator();
                        ide.Reset();
                        while (ide.MoveNext())
                        {
                            DeviceParam d = ((DeviceView)ide.Value).deviceparam;
                            RoadParam r = ((DeviceView)ide.Value).roadparam;
                            dataGridView1.Rows.Add(new object[] { d.roadid, r.roadname, d.ip, d.type.ToString(), d.workmode.ToString(), d.direction.ToString(), d.sbbh, d.speedlimit, d.deviceusername, d.devicepassword, d.mutiroad.ToString(), d.channel1roadid, d.channel2roadid, d.channel3roadid, d.channel4roadid, d.channel5roadid, d.channel6roadid, d.channel7roadid, d.channel8roadid, d.djsnapbyplate.ToString(), d.platetypesnapby.ToString(), d.djsnapbytime.ToString(), d.time1_start, d.time1_end, d.time2_start, d.time2_end, d.time3_start, d.time3_end, d.time4_start, d.time4_end,d.staticpicenable.ToString(),d.staticpicindex.ToString(),d.staticpic, d.bz,d.picwidth.ToString(),d.picheight.ToString() });
                        }
                        ((Form_Main)this.Owner).UpDateTree();
                        MessageBox.Show("同步成功");
                    }
                    else
                    {
                        MessageBox.Show("同步失败");
                    }
                }
                else
                {
                    MessageBox.Show("同步器未启动");
                }
            }
            catch (Exception er)
            {
                MessageBox.Show("同步时出现异常" + er.Message);
            }
        }

        private bool OnDeviceSynFunc(ref Msg send, Msg read, ref string error)
        {
            bool ans = false;
            try
            {
                if ((send != null) && (read != null))
                {
                    if ((send.type == Enum_ProtocolType.DeviceRequest) && (read.type == Enum_ProtocolType.DeviceResponse))
                    {
                        if (send.uuid == read.uuid)
                        {
                            string DataRecvIP = Encoding.UTF8.GetString(send.data);
                            DeviceSyn response = DeviceSyn.Parse(read.data);
                            if (response != null)
                            {
                                if ((DataRecvIP == response.DataRecvIP) && (DataRecvIP == G.SystemParam.LocalIP))
                                {
                                    if ((response.Count > 0) && (G.Roads != null))
                                    {
                                        G.Views.Clear();
                                        foreach (DeviceNode node in response.DeviceNodeList)
                                        {
                                            DeviceParam dp = new DeviceParam();
                                            dp.ip = node.ip;
                                            dp.roadid = node.roadid;
                                            dp.sbbh = node.sbbh;
                                            try
                                            {
                                                dp.type = (Enum_DeviceType)Enum.Parse(typeof(Enum_DeviceType), node.sblx);
                                            }
                                            catch{}
                                            try
                                            {
                                                dp.direction = (Enum_Direction)Enum.Parse(typeof(Enum_Direction), node.direction);
                                            }
                                            catch{}
                                            dp.deviceusername = node.username;
                                            dp.devicepassword = node.password;
                                            DeviceParam.AddModel(dp);
                                        }
                                        G.Views = DeviceView.GetModels();
                                        ans = true;
                                    }
                                }
                                else
                                {
                                    error = "数据终端IP错误";
                                }
                            }
                        }
                        else
                        {
                            error = "uuid不一致";
                        }
                    }
                    else
                    {
                        error = "消息类型错误";
                    }
                }
            }
            catch (Exception er)
            {
                ans = false;
                error = er.Message;
            }
            return ans;
        }

        private void camera_FormClosed(object sender, FormClosedEventArgs e)
        {
            try
            {
                if (synclient != null)
                {
                    synclient.Dispose();
                }
            }
            catch { }
        }

    }
}
